<?php

// Model:'Don' - Database Table: 'bieu_mau' - Đơn thanh tra

Class Don extends Eloquent {

    //Bang trong csdl
    private $nameTable = 'bieu_mau';
    //Rang buoc du lieu
    private $rules = array(
        'tendon' => 'required|min:3|max:100',
        'noidung' => 'required'
    );
    //So don hien thi tren mot trang
    private $don = 10;

    //Ham kiem tra du lieu
    public function validation($data) {
        return Validator::make($data, $this->rules);
    }

    //Lay so du lieu hien thi tren trang
    public function GetDon() {
        return $this->don;
    }

    //Lay ds du lieu
    public function GetList($key) {
        $data = null;
        if ($key === null) {
            $data = DB::table($this->nameTable)
                    ->select("*")
                    ->orderBy("TEN_BIEU_MAU")
                    ->paginate($this->don);
        } else {
            $data = DB::table($this->nameTable)
                    ->select("*")
                    ->where("TEN_BIEU_MAU", "LIKE", "%" . $key . "%")
                    ->orderBy("TEN_BIEU_MAU")
                    ->paginate($this->don);
        }

        return $data;
    }

    //Lay du lieu xac dinh
    public function GetOne($id) {
        return DB::table($this->nameTable)
                        ->select("*")
                        ->where("ID", $id)
                        ->first();
    }

    //Luu du lieu sua doi
    public function StoreIn($data, $id) {
        $check = DB::table($this->nameTable)
                ->select("*")
                ->where("MSCB", $data["MSCB"])
                ->where("ID", "<>", $id)
                ->first();
        if (!is_null($check)) {
            return false;
        } else {
            return DB::table($this->nameTable)
                            ->where("ID", $id)
                            ->update($data);
        }
    }

    //Xoa du lieu xac dinh
    public function Remove($id) {
        try {
            return DB::table($this->nameTable)
                            ->delete($id);
        } catch (Exception $ex) {
            return false;
        }
    }
    
    //Lay du lieu xac dinh
    public function GetOneWithMST($tend) {
        return DB::table($this->nameTable)
                        ->select("*")
                        ->where("ID", $tend)
                        ->first();
    }
}
